import {SHOPPING_CART, USER_INFO} from "@/common/constant";

export default {
    addAllToShopCart(state, list) {
        state.shopCartList = list;
    },
    addNewProducts(state, payload) {
        state.shopCartList.push(payload);
        let userInfo = JSON.parse(localStorage.getItem(USER_INFO));
        if(userInfo) {
            localStorage.setItem(SHOPPING_CART + "-" + userInfo.phone, JSON.stringify(state.shopCartList));
        }
    },
    changeGoodsCount(state, {item, purchaseCount}) {
        item.purchaseCount += purchaseCount;
        let userInfo = JSON.parse(localStorage.getItem(USER_INFO));
        if(userInfo) {
            localStorage.setItem(SHOPPING_CART + "-" + userInfo.phone, JSON.stringify(state.shopCartList));
        }
    },
    changeGoodsCheck(state, item) {
        item.checked = !item.checked;
    },
    selectAllCheck(state) {
        state.shopCartList.forEach(item => item.checked = true);
    },
    noSelectAllCheck(state) {
        state.shopCartList.forEach(item => item.checked = false);
    }
}